/**
 * @Description: 处理饼状图
 * @Author bianpengfei
 * @create 2022/9/16 10:35
 */
import { formatAxisLabel, formatNum } from '@base/chart/utils'

export default {
  handler() {
    const [data] = arguments

    const seriesData = this.data.map((v) => ({ ...v, name: v.name, value: v.valuey1 }))
    const seriesUnits = this.seriesUnits
    const seriesColors = this.seriesColors
    // const enhancerValueY1 = this._enhancerDataObj['valuey1']
    // const enhancerValueY2 = this._enhancerDataObj['valuey2']
    // const enhancerValueZ1 = this._enhancerDataObj['valuez1']
    // const enhancerValueZ2 = this._enhancerDataObj['valuez2']

    return {
      tooltip: {
        show: true,
        trigger: 'item',
        formatter: '{b}：{d}%',
        backgroundColor: 'rgba(0,0,0,0.7)', // 设置背景图片 rgba格式
        borderWidth: 0,
        padding: [8, 10],
        textStyle: {
          color: '#fff', // 设置文字颜色
          fontSize: 12
        }
      },
      legend: {
        left: 'center',
        itemWidth: 8,
        itemHeight: 8,
        top: '2%'
      },
      series: [
        {
          color: seriesColors,
          type: 'pie',
          // silent: true,
          radius: ['42%', '60%'],
          center: ['50%', '55%'],
          data: seriesData,
          clockwise: true,
          label: {
            show: true,
            fontSize: 8,
            overflow: 'break',
            // position: 'inner',
            // show: false,
            // margin: 10,
            // alignTo: 'edge',
            // position:'outer',
            formatter: (val) => {
              let { name, percent, dataIndex, data } = val
              const label = formatAxisLabel(name, 5)
              const unit = seriesUnits[dataIndex] || seriesUnits.at(0) || ''
              const num = formatNum(data.value, 2)
              return [`{${dataIndex}|${label}${unit ? num + unit : ''}}`, `{${dataIndex}|占${percent}%}`].join('')
            },
            textStyle: {
              color: '#333'
            },
            rich: seriesColors.reduce((p, c, idx) => {
              p[String(idx)] = {
                fontSize: 12,
                color: '#333'
              }
              return p
            }, {})
          },
          emphasis: {
            label: {
              show: true,
              fontWeight: 'normal'
            }
          },
          labelLine: {
            show: true,
            length: 2,
            normal: {
              length: 10,
              length2: 10,
              lineStyle: {
                width: 1
              }
            }
          }
        }
      ]
    }
  }
}
